"""
    Plugin for viewing content from nrk.no
"""


#main imports
import os
import sys
import xbmc


#Compability with both old plugin system and new addon system
#Quick and ugly..
isaddon   = False
isplugin  = False
isconsole = False

try: 
    import xbmcaddon
except:  
    try:    import xbmcplugin
    except: isplugin = False
    else:   isplugin= True
else:    
    isaddon= True
    isconsole = (isaddon== False and isplugin== False)
   

if isaddon== True:
    __settings__ = xbmcaddon.Addon(id='plugin.video.nrk')
    __language__ = __settings__.getLocalizedString
elif isplugin== True:
    __settings__ = xbmcplugin
    __language__ = xbmc.getLocalizedString
    
import NRK_API.utils as utils
from NRK_API.utils import Log, PluginError, PluginScriptError

#plugin constants
__plugin__         = "NRK"
__author__         = "VictorV"
__version__        = "0.9.8"
__XBMC_Revision__  = "21735"



def run_once():
    runonce = __settings__.getSetting('runonce') == 'true'
    __settings__.openSettings(url=sys.argv[0])
    __settings__.setSetting('runonce', 'True')
    
    
def _check_compatible():
    #spam plugin statistics to log
    xbmc.log("addon :: title '%s, version %s'" % (__plugin__, __version__,), xbmc.LOGNOTICE) 
    
    if utils.Plugin.get_platform() == utils.XBOX:
        xbmc_rev = 0
        ok = 2
    else:
        try:
            xbmc_rev = int( xbmc.getInfoLabel( "System.BuildVersion" ).split( " r" )[ -1 ] )
            #compatible?
            ok = xbmc_rev >= int( __XBMC_Revision__ )  
        except:
            #error, so unknown, allow to run
            xbmc_rev = 0
            ok = 2
        
    #spam revision info
    xbmc.log( "addon.NRK.init :: Required XBMC Revision: r%s" % (
                 __XBMC_Revision__), xbmc.LOGNOTICE )
    xbmc.log( "addon.NRK.init :: Found XBMC Revision: r%d [%s]" % ( 
                 xbmc_rev, ("Not Compatible", "Compatible", "Unknown")[ok]), 
                 xbmc.LOGNOTICE )
                 
    #if not compatible, inform user
    if ( not ok ):
        import xbmcgui
        xbmcgui.Dialog().ok( "%s - %s: %s" % (
                 __plugin__, __language__( 30700 ), __version__), 
                 __language__( 30701 ) % (__plugin__),
                 __language__( 30702 ) % (__XBMC_Revision__),
                 __language__( 30703 ) )
                 
    #return result
    return ok



if (__name__ == "__main__"):

    if (not sys.argv[2]):
        if False == isaddon:
            ok = _check_compatible()
        else: ok = 1
        if ( (__settings__.getSetting('runonce') == 'true') == True ) and ( utils.Plugin.get_platform() != utils.XBOX ):
            run_once()
        if (ok):
            from NRK_API import xbmcplugin_rootmenu as plugin
            Log.debug('addon.NRK.import :: rootmenu')
            
    else:
        if sys.argv[2][1:].startswith('program'):
            from NRK_API import xbmcplugin_program as plugin
            Log.debug('addon.NRK.import :: program')
            
        elif sys.argv[2][1:].startswith('nrkbeta'):
            from NRK_API import xbmcplugin_nrk_beta_feeds as plugin
            Log.debug('addon.NRK.import :: nrkbeta')
            
        elif sys.argv[2][1:].startswith('other'):
            from NRK_API import xbmcplugin_other as plugin
            Log.debug('addon.NRK.import :: other')

        elif sys.argv[2][1:].startswith('webradio'):
            from NRK_API import xbmcplugin_webradio as plugin
            Log.debug('addon.NRK.import :: webradio')
            
        elif sys.argv[2][1:].startswith('podcast'):
            from NRK_API import xbmcplugin_podcast as plugin
            Log.debug('addon.NRK.import :: podcast')
            
        elif sys.argv[2][1:].startswith('teletext'):
            from NRK_API import xbmcplugin_teletext as plugin
            Log.debug('addon.NRK.import :: teletext')
    
        elif sys.argv[2][1:].startswith('kanalene'):
            from NRK_API import chlive as plugin
            Log.debug('addon.NRK.import :: channels')
        
        elif sys.argv[2][1:].startswith('favorites'):
            from NRK_API import favorites as plugin
            Log.debug('addon.NRK.import :: favorites')
      
        elif sys.argv[2][1:].startswith('nogui'):
            from NRK_API import nogui as plugin
            Log.debug('addon.NRK.import :: nogui')
        
        else: 
            raise PluginError('Key error', 'addon.NRK :: %s unknown key prefix' % __file__)
    try:
        plugin.Main()
    except PluginError:
        Log.error('Plugin encountered a error retreiving virtual directory')
    
    #Use plugin error class for unknown errors
    except:
        #need a try/except clause to avoid xbmc error dialog
        try:
            raise PluginScriptError
        except: 
            Log.error('Plugin encountered a error retreiving virtual directory')

        
        
        
        
        
